home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 45975 / 45975.xpi / content / utils.js < prev    next >
Text File  |  2009-11-23  |  4KB  |  100 lines

  1. /*
  2.  * Copyright (c) 2009 Bui Viet Thanh (thanhbv@gmail.com).
  3.  *
  4.  * This file is part of clicknlearn.
  5.  *
  6.  * clicknlearn is free software: you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation, either version 3 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * clicknlearn is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with clicknlearn.  If not, see <http://www.gnu.org/licenses/>.
  18.  */
  19.  
  20. let CNLUtils = {
  21.     /**
  22.      * search(vector: Array, value: Object, [insert: Boolean = false]): Integer
  23.      * Do a binary search on an *ordered* array, if it's not ordered, the behaviour is undefined.
  24.      *   The function can return the index of the searched object as well the the index where it should be
  25.      *   inserted to keep the array ordered.
  26.      *   @param o vector
  27.      *       array that will be looked up
  28.      *   @param v value
  29.      *       object that will be searched
  30.      *   @param i insert
  31.      *       if true, the function will return the index where the value should be inserted
  32.      *       to keep the array ordered, otherwise returns the index where the value was found
  33.      *       or -1 if it wasn't found
  34.      * Carlos R. L. Rodrigues
  35.      * http://jsfromhell.com/array/search [rev. #2]
  36.      */
  37.     search: function(o, v, i){
  38.         var h = o.length, l = -1, m;
  39.         while(h - l > 1)
  40.             if(o[m = h + l >> 1] < v) l = m;
  41.             else h = m;
  42.         return o[h] != v ? i ? h : -1 : h;
  43.     },
  44.  
  45.     formatWord: function(word){
  46.         word = word.replace(/^ +| +$/g, ""); // Remove empty spaces around words
  47.         word = word.replace(/^'+/g, ""); // Remove ' or ∩┐╜ at the start of the word
  48.         word = word.replace(/'+$|∩┐╜+$/g, ""); // Remove ' or ∩┐╜ at the end of the word
  49.         word = word.replace(/\.|,|"|:|;|!|\?|\(|\)|\//g, "");
  50.         return word.toLowerCase();
  51.     },
  52.     /**
  53.      * replace '%%' in str by element in array arr
  54.      */
  55.     replaceString: function(str, arr){
  56.         return str.split(/%%/).reduce(function(previousValue, currentValue, index, array){
  57.               return previousValue + arr[index-1] + currentValue ;
  58.             });
  59.     },
  60.     /**
  61.      * ANT-style variable replacing in strings
  62.      */
  63.     replaceAntStyleString: function(str){
  64.         var re = /\$\{(.+?)\}/mg;
  65.         var reArr;
  66.         var ret = "";
  67.         var prevIndex = 0;
  68.         while ((reArr = re.exec(str)) != null){
  69. //          ex: str = "1${a}2"
  70. //            then property = reArr[1] = "a";
  71. //          & reArr[0] = ${a}           
  72.             ret += str.substring(prevIndex, re.lastIndex - reArr[0].length) +
  73.                    CNL.stringsBundle.getString(reArr[1]);            
  74.             prevIndex = re.lastIndex;
  75.         }
  76.         ret += str.substring(prevIndex);
  77.         return ret;
  78.     },
  79.     getString: function(str, arr){
  80.         let temp = CNLUtils.replaceAntStyleString(str);
  81.         return CNLUtils.replaceString(temp, arr);
  82.     },
  83.  
  84.     VALID_CHARS: /[0-9a-zA-Z╨░-╤Å╨É-╨»├á-┼╛╬æ-ß┐Ñ╘▒-╓çπüü-πâ╢Σ┐â-µ¥üπä▒-φòÿΓü┐'ΓÇÖ┼┐├ü├ë-]/,
  85.  
  86.     trim: function(str, chars) {
  87.         return this.ltrim(this.rtrim(str, chars), chars);
  88.     },
  89.  
  90.     ltrim: function(str, chars) {
  91.         chars = chars || "\\s";
  92.         return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
  93.     },
  94.  
  95.     rtrim: function(str, chars) {
  96.         chars = chars || "\\s";
  97.         return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
  98.     }
  99. };
  100.